#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;

pthread_cond_t cond;
pthread_mutex_t mutex;
void* r1(void* args)
{
	while(1)
	{
		pthread_cond_wait(&cond,&mutex);
		cout<<(char*)args<<": 活动..." << endl;
	}
	return nullptr;
}
void* r2(void* args)
{
	while(1)
	{
		pthread_cond_signal(&cond);
		sleep(1);
	}
	return nullptr;
}
int main()
{
	pthread_cond_init(&cond,nullptr);
	pthread_mutex_init(&mutex,nullptr);
	pthread_t tid1,tid2,tid3,tid4;

	pthread_create(&tid1,nullptr,r1,(void*)"thread1");
	pthread_create(&tid2,nullptr,r1,(void*)"thread2");
	pthread_create(&tid3,nullptr,r1,(void*)"thread3");
	pthread_create(&tid4,nullptr,r2,(void*)"thread4");

	pthread_join(tid1,nullptr);
	pthread_join(tid2,nullptr);
	pthread_join(tid3,nullptr);
	pthread_join(tid4,nullptr);

	pthread_cond_destroy(&cond);
	pthread_mutex_destroy(&mutex);
	return 0;
}
